package controller;

import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Stateless;

import utils.DataMiner;
import utils.PrinterStatusBeanRemote;
import utils.TechresourceDAO;
import dtos.TechresourceDTO;

/**
 * Session Bean implementation class PrinterStatusBean
 */
@Stateless
public class PrinterStatusBean implements PrinterStatusBeanRemote {
	private final static Logger LOGGER = Logger.getLogger(DataMiner.class
			.getName());
	protected DataMiner dm;
	
	protected int pagesDifference = 0;
	protected int pagesOldTotal = 0;
	protected int pagesOldCurrent = 0;
	protected int currentTotalValue = 0;
	
	@EJB
	TechresourceDAO trd;

	public PrinterStatusBean() {
	}

	@PostConstruct
	public void init() {
		dm = new DataMiner();
	}

	public String packDeviceStatus() {
		currentTotalValue = dm.getNrOfPages();
		StringBuffer result = new StringBuffer();
		Map<String, Integer> colorAmountMap = dm.getColorValues();
		for (String key : dm.getColorValues().keySet()) {
			result.append(colorAmountMap.get(key) + ",");
		}
		result.append(dm.getNrOfPages()+",");
		pagesDifference = currentTotalValue - pagesOldTotal;
		LOGGER.log(Level.INFO, "----------         OLD  =    " + pagesOldTotal + "        NEW =     "+ dm.getNrOfPages()  );
		result.append(pagesDifference);
		LOGGER.log(Level.INFO, "---------- PACK DEVICE STATUS:      Result    =    " + result.toString());
		return result.toString();
	}

	/**
	 * Unpacks TechResource
	 * 
	 * @return List of string containing device status inforamtions
	 */
	public void unPackDeviceStatus(TechresourceDTO res) {
		if (!res.getStatus().equals("Working")
				&& !res.getStatus().equals("Not Working")) {
			LOGGER.log(Level.INFO, "--------------- unPackDeviceStatus:    start");
			String status = res.getStatus();
			String[] colorsAndVal = status.split(",");
			pagesOldTotal = Integer.parseInt(colorsAndVal[4].trim());
//			pagesOldCurrent = Integer.parseInt(colorsAndVal[5].trim());
			LOGGER.log(Level.INFO, "================ UNPACK STATUS:      Result    =   " +pagesOldTotal + "   " + "dif old = "  + pagesOldCurrent   );
		}
	}

	/**
	 * Calculates dai2ly report printed-pages-value and refreshes Cartiges % in
	 * db
	 * 
	 * @param ip
	 *            - valid http enabled device
	 * @throws IOException
	 */
	public String calculateDeviceStats(String ip) throws IOException {
		LOGGER.log(Level.INFO, "calculateDeviceStats - START with IP: " + ip);
		String newIp=ip.substring(7, ip.length());
		TechresourceDTO res = trd.getTechResourcesDTOByIp(newIp).get(0);
		LOGGER.log(Level.INFO, "**(*(*(*(*(*(*" + ip);
		unPackDeviceStatus(res);
		dm.buildCartigesAndValue(ip);
		dm.buildNrOfPagesToday(ip);
		LOGGER.log(Level.INFO, "**(*(*(*(*(*(*" + ip);
		String newStatus = packDeviceStatus();
	    res.setStatus(newStatus);
		LOGGER.log(Level.INFO, "calculateDeviceStats - END --------------------------- \n\n\n old "+ pagesOldTotal + "new " + dm.getNrOfPages()+ " dif " + pagesDifference);
		return newStatus;
	
	}
}

